package de.sendorian.util.matcher;

import org.apache.commons.lang.ObjectUtils;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;


@SuppressWarnings("unchecked")
public class BlacklistMatcher<T> extends TypeSafeDiagnosingMatcher<T> {

    private Iterable unwantedObjects;


    protected BlacklistMatcher() {
    }


    public BlacklistMatcher(Iterable unwandetObjects) {
        setUnwantedObjects(unwandetObjects);
    }


    @Override
    protected boolean matchesSafely(T item, Description mismatchDescription) {
        for (Object unwantedObject : unwantedObjects) {
            if (matchesInternal(item, unwantedObject))
                return true;
        }
        return false;
    }


    protected boolean matchesInternal(T objectToCompare, Object unwantedObject) {
        return ObjectUtils.equals(objectToCompare, unwantedObject);
    }


    public void describeTo(Description description) {
        description.appendText("contained in ").appendValue(unwantedObjects);

    }


    protected void setUnwantedObjects(Iterable unwantedObjects) {
        this.unwantedObjects = unwantedObjects;
    }

}
